<template>
  <div>
    <div class="table-page-search">
      <a-form>
        <a-form-item label="名称">
          <a-input placeholder="请输入" v-model="queryParam.keyWord" />
        </a-form-item>
        <a-form-item label="状态">
          <a-select placeholder="请选择" v-model="queryParam.status">
            <a-select-option :value="''">全部</a-select-option>
            <a-select-option :value="1">正常</a-select-option>
            <a-select-option :value="0">禁用</a-select-option>
          </a-select>
        </a-form-item>
        <span class="search-submitButtons">
          <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
          <a-button @click="() => (queryParam = { status: '' })">重置</a-button>
        </span>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button type="primary" @click="handleAdd">新增</a-button>
    </div>
    <s-table size="default" ref="table" rowKey="userId" :rowSelection="null" :columns="columns" :data="loadData">
      <span slot="serial" slot-scope="text, record, index">
        {{ (pageNum - 1) * pageSize + index + 1 }}
      </span>
      <span slot="status" slot-scope="text, record">
        <a-switch :checked="record.enabled === 1" @change="onChangeStatus(record)" />
      </span>
      <span slot="action" slot-scope="text, record">
        <a-button type="primary" ghost size="small" class="_mr-5" @click="handleEdit(record)"> 编辑 </a-button>
        <a-button type="primary" ghost size="small" class="_mr-5" @click="delByIds([record.id])"> 删除 </a-button>
      </span>
    </s-table>
    <Form ref="form" @add="$refs.table.refresh(true)" @upd="$refs.table.refresh()" />
  </div>
</template>

<script>
import { STable } from '@/components'
import { memberRatingList, memberRatingEnable, memberRatingDelete } from '@/api/system/system'
import Form from './modules/Form'
export default {
  name: 'GradeEvaluation',
  components: {
    STable,
    Form,
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      // 查询参数
      queryParam: { status: '' },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width: '80px',
        },
        {
          title: '名称',
          dataIndex: 'name',
          width: '200px',
          ellipsis: true,
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: '200px',
          scopedSlots: { customRender: 'status' },
        },
        {
          title: '排序',
          dataIndex: 'sort',
          width: '80px',
          ellipsis: true,
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: '200px',
          ellipsis: true,
        },
        {
          title: '操作',
          width: '150px',
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageNum: 1,
      pageSize: 10,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return memberRatingList(Object.assign({ ...parameter, size: parameter.pageSize }, this.queryParam))
      },
    }
  },
  created() {},
  methods: {
    handleAdd() {
      var params = {
        title: '新增等级评估',
        type: 'add',
      }
      this.$refs.form.add(params)
    },
    handleEdit(record) {
      var params = {
        title: '编辑等级评估',
        type: 'edit',
        data: record,
      }
      this.$refs.form.edit(params)
    },

    handleOk() {
      this.$refs.table.refresh(true)
    },
    handleReload() {
      this.$refs.table.refresh()
    },
    delByIds(ids) {
      var _this = this
      this.$confirm({
        title: '删除?',
        content: '是否确定删除该等级评估?',
        onOk() {
          memberRatingDelete({ ids: ids.join(',') }).then((res) => {
            if (res.code === 0) {
              _this.$message.success(`删除成功`)
              _this.handleReload()
            } else {
              _this.$message.error(res.msg)
            }
          })
        },
        onCancel() {},
      })
    },

    onChangeStatus(record) {
      var msg = ''
      if (record.enabled === 0) {
        msg = '启用成功'
      } else {
        msg = '禁用成功'
      }
      record.status = record.enabled === 0 ? 1 : 0
      memberRatingEnable(record).then((res) => {
        if (res.code === 0) {
          this.$message.success(msg, 1)
          this.$refs.table.refresh()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  },
}
</script>
